package other;

import java.util.Scanner;

/*

4
A,B,C,D
A,B,E,F
A,C,E,F
B,C,D,E
4
A,B,C,E
A,B,D,F
A,C,E,K
A,C,D,E


2
A,B,C,D
A,H,J,K
2
A,B,C,E
A,B,C,F

结果：ABCDE  ABCDF
 *
 */


public class StringPro {

	static String[] table1 = new String[100];    //表A数据
	static String[] table2 = new String[100];    //表B数据
	static String[] table = new String[100];     //A、B合并表

	public static void main(String[] args) {

		Scanner sc = new java.util.Scanner(System.in);
		int len1 = sc.nextInt();  //表1中有n条数据
		table1 = GetTable(table1,len1);

		int len2 = sc.nextInt();  //表1中有n条数据
		table2 = GetTable(table2,len2);
		int t=0;    //合并table中的t条数据
		for (int i=0;i<len1;i++) {
			String t1 = table1[i].substring(0, table1[i].length()-1);   //取表A前len-1个字符
			for (int j=0;j<len2;j++) {
				String t2 = table2[j].substring(0, table2[j].length()-1);  //取表B前len-1个字符
				if(t1.equals(t2))
				{
					char x = table1[i].charAt(table1[i].length()-1);  //返回某条数据最后一项
					char y = table2[j].charAt(table2[i].length()-1);
					if(x<=y){   //比较表1、表2最后一项数据先后
						table[t] = t1 + x + y;
					}else{
						table[t] = t1 + y + x;
					}
					t++;
				}
			}
		}
		for(int i=0;i<t;i++) {
			String regex = "(.{1})";   //每隔一个字符穿插符号
			table[i] = table[i].replaceAll(regex,"$1,");
			table[i] = table[i].substring(0,table[i].length() - 1);   //去掉最后一个逗号
			System.out.println(table[i]);	// 输出
		}

	}


	public static String[] GetTable(String[] table,int len) {
		String remove = ",";
		Scanner sc1 = new Scanner(System.in);
		for(int i=0;i<len;i++){
			String str = sc1.nextLine();
			str = str.replace(remove, "");   //对数据的逗号进行处理
			table[i] = str;
		}
		return table;
	}

}
